<?php
/**
 * 公司客户统计
 */

namespace app\crontab\controller\always;


use custom\Log;
use think\console\Command;
use think\console\Input;
use think\console\Output;
use think\Db;

class  CompanyStatistics extends Command {

    protected function configure() {
        $this->setName('CompanyStatistics');
    }

    protected function execute(Input $input, Output $output) {
        $this->initial();
    }

    public function initial() {
        // 关闭日志写入
        \think\facade\Log::close();
        echo "starting\n";
        date_default_timezone_set('PRC');
        $yestaday = strtotime(date('Y-m-d', strtotime('-1 day')));

        //昨日新增专属客户
        $exclusive_customer_number = "(select count(id) from erp_company_user where erp_company_user.company_id=erp_company.id and daytime_exclusive = {$yestaday}) as exclusive_customer_number";
        //昨日新增潜在客户
        $customer_number = "(select count(id) from erp_company_user where erp_company_user.company_id=erp_company.id and daytime_potential = {$yestaday}) as customer_number";
        $data1 = Db::query("select id,{$exclusive_customer_number},{$customer_number} from erp_company where deleted=0");

        //上次累计潜在客户
        $customer_total_number = "(select customer_total_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id order by day_time desc limit 0,1) as customer_total_number";
        //上次累计专属客户
        $exclusive_customer_total_number = "(select exclusive_customer_total_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id order by day_time desc limit 0,1) as exclusive_customer_total_number";
        //上次累计7天潜在客户
        $customer_seven_number = "(select customer_seven_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id order by day_time desc limit 0,1) as customer_seven_number";
        //上次累计7天专属客户
        $exclusive_customer_seven_number = "(select exclusive_customer_seven_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id order by day_time desc limit 0,1) as exclusive_customer_seven_number";
        $data2 = Db::query("select {$customer_total_number},{$exclusive_customer_total_number},{$customer_seven_number},{$exclusive_customer_seven_number} from erp_company where deleted=0");

        $seven_before = strtotime(date('Y-m-d', strtotime('-8 day')));
        //往前推8天当天新增潜在客户
        $seven_customer_number = "(select customer_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id and day_time = {$seven_before}) as seven_customer_number";
        //往前推8天当天新增专属客户
        $seven_exclusive_customer_number = "(select exclusive_customer_number from erp_company_statistics where erp_company_statistics.company_id=erp_company.id and day_time ={$seven_before}) as seven_exclusive_customer_number";
        $data4 = Db::query("select {$seven_customer_number},{$seven_exclusive_customer_number}  from erp_company where deleted=0");

        foreach ($data1 as $k1 => $v1) {
            $insert = [
                'company_id' => $data1[$k1]['id'],

                'customer_number'       => $data1[$k1]['customer_number'],
                'customer_total_number' => $data1[$k1]['customer_number'] + $data2[$k1]['customer_total_number'],
                'customer_seven_number' => ($data2[$k1]['customer_seven_number'] - $data4[$k1]['seven_customer_number']) + $data1[$k1]['customer_number'],

                'exclusive_customer_number'       => $data1[$k1]['exclusive_customer_number'],
                'exclusive_customer_total_number' => $data1[$k1]['exclusive_customer_number'] + $data2[$k1]['exclusive_customer_total_number'],
                'exclusive_customer_seven_number' => ($data2[$k1]['exclusive_customer_seven_number'] - $data4[$k1]['seven_exclusive_customer_number']) + $data1[$k1]['exclusive_customer_number'],
                'itime'                           => time(),
                'day_time'                        => strtotime(date('Y-m-d', strtotime('-1 day'))),
            ];
            $res[] = $insert;
        }
        $total = count($res);
        $suc = Db::name('company_statistics')->data($res)->limit(100)->insertAll();
        echo 'success->' . $suc . PHP_EOL;
        echo 'total->' . $total . PHP_EOL;
        echo "end" . PHP_EOL;
        $res = json_encode($res);
        Log::write("success->$suc;total->$total;res:$res", 'company_statistics.log');
    }

}